package com.bkgy.commom.model;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Map;

public class CExportExcelView extends AbstractXlsView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        workbook = (XSSFWorkbook) model.get("workBook");
        String fileName = (String) model.get("fileName");
        response.setContentType("application/xls");
        response.setCharacterEncoding("UTF-8");
        String dfileName = null;
        dfileName = new String(fileName.getBytes("UTF-8"), "ISO_8859_1");
        response.setHeader("Content-disposition", "attachment;filename=" + dfileName);
        OutputStream ouputStream = response.getOutputStream();
        workbook.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}